<template>
  <el-container>
    <!-- //头部 -->
    <el-header>
      <div>
        <img src="../assets/img/hmbb.jpeg" alt="" srcset="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="quit">退出登录</el-button>
    </el-header>
    <!-- //主体 -->
    <el-container>
      <!-- //左边栏 -->
      <el-aside :width="widthData">
        <div class="toggle-button" @click="toggleClickk">|||</div>
        <!-- 左边栏菜单区域 -->
        <el-menu
          background-color="#363D40"
          text-color="#fff"
          active-text-color="#009EFF"
          unique-opened
          :collapse="collapseData"
          :collapse-transition="false"
          router
          :default-active="$route.path"
        >
          <!--第一行-->
          <!-- :index是唯一标识 如果没有会一次展开全部页面
          在路由功能一块  他可以指定跳转到那个页面-->
          <el-submenu
            :index="titleName.id + ''"
            v-for="(titleName, index) in menulist"
            :key="titleName.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[index]"></i>
              <!-- 文本 -->
              <span> {{ titleName.authName }} </span>
            </template>

            <!-- 二级菜单模板区域 -->
            <el-menu-item
              v-for="titlesName_fuben in titleName.children"
              :index="'/' + titlesName_fuben.path"
              :key="titlesName_fuben.id"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ titlesName_fuben.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- //右边栏 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menulist: [],
      iconObj: {
        0: "iconfont icon-user",
        1: "iconfont icon-tijikongjian",
        2: "iconfont icon-shangpin",
        3: "iconfont icon-danju",
        4: "iconfont icon-baobiao",
      },
      collapseData: false,
      widthData: "200px",
    };
  },
  methods: {
    quit() {
      this.$confirm("确定退出吗？？？？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "退出成功!",
          });
          window.sessionStorage.clear();
          this.$router.replace("/login");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消退出",
          });
        });
    },
    // 获取搜有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },

    toggleClickk() {
      this.collapseData = !this.collapseData;
      this.collapseData
        ? (this.widthData = "64px")
        : (this.widthData = "200px");
    },
  },
  created() {
    this.getMenuList();
  },
};
</script>

<style scoped>
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 6px;
}
section {
  height: 100%;
  /* border: 1px solid red; */
}
header button {
  height: 70%;
}
header div {
  display: flex;
  align-items: center;
}
header div span {
  margin-left: 30px;
  display: inline-block;
  color: rgb(247, 250, 252);
  cursor: pointer;
}
header div span:hover {
  color: rgba(247, 250, 252, 0.6);
}
header div img {
  border-radius: 50%;
  height: 50px;
  width: 50px;
}
.el-header {
  background-color: #363d40;
}
.el-aside {
  background-color: #363d40;
}
.iconfont {
  margin-right: 13px;
}
.el-menu {
  border: none;
}
.toggle-button {
  background-color: #4a5064;
  color: white;
  text-align: center;
  line-height: 25px;
  cursor: pointer;
  font-size: 10px;
  letter-spacing: 0.3em;
}
.el-main {
  background-color: #e9edf1;
}
</style>